package com.cyz.springdemo.Controller.api;

import com.cyz.springdemo.model.BaseResponse;
import com.cyz.springdemo.util.EncryptionUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: springdemo
 * @description: 获取网络时间接口控制器
 * @author: wangjie
 * @create: 2019-07-25 11:26
 **/
@RestController
@RequestMapping(value = "/demoTest")
public class WebSiteTimeController {

    @CrossOrigin
    @RequestMapping(value = "/createPanel",method = RequestMethod.GET)
    public BaseResponse createPanel() throws IOException {
        BaseResponse response = new BaseResponse();

        try {
            URL url = new URL("http://time.tianqi.com/");
            URLConnection uc = url.openConnection();
            uc.connect();
            Long ld = uc.getDate();
            Date date = new Date(ld);

            SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
            String dateTime = sdf.format(date);
            //对进去的时间进行加密输出
            String timeNew = EncryptionUtil.dateEncry(dateTime);

            StringBuffer sb = new StringBuffer(timeNew);
            sb.insert(2,",");       //在1的位置之后插入,逗号

            //response.setData(dateTime);
            response.setMessage(sb.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return response;
    }
}
